﻿
function RouteCompareChartWindow() {
    if (Ext.getCmp('route_compare_chart_window')) {
        Ext.getCmp('route_compare_chart_window').show();
        return;
    }

    var trackerComboBox = getTrackerCheckboxCombo(true);
    var routeFields = ["real_route", "defined_route"];
    var routeStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchTrackerRoutes",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: routeFields,
            root: "data"
        }),
        autoLoad: false
    });
    var day = new Date();
    var today = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate();
    var toDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today,
        editable: false
    });

    var tbar = new Ext.Toolbar({
        items: [
            trackerComboBox, {
                xtype: 'tbseparator', width: 5
            }, toDateEditor, {
                xtype: 'tbseparator', width: 5
            }, {
                tooltip: { text: _POI_Search, autoHide: true },
                icon: 'images/search16.gif',
                handler: function() {
                    routeStore.load({
                        params: {
                            ids: trackerComboBox.getValue(),
                            fromTime: toDateEditor.getEl().dom.value + " 00:00:00",
                            toTime: toDateEditor.getEl().dom.value + " 23:59:59",
                            trackerNames: trackerComboBox.getRawValue()
                        },
                        callback: function(records, options, success) {
                            if (success) {
                                routecomparewindow.removeAll(true);
                                if (records.length > 0) {
                                    for (var i = 0; i < records.length; i++) {
                                        var title = "";
                                        for (var j = 0; j < trackerComboBox.getStore().getCount(); j++) {
                                            var r = trackerComboBox.getStore().getAt(j);
                                            if (records[i].get('real_route').sn_imei_id == r.get('snid')) {
                                                title = r.get('trackname');
                                                break;
                                            }
                                        }
                                        var routeCompareChartPanel = new Ext.ux.RouteCompareChartPanel({
                                            defined_route: records[i].get('defined_route'),
                                            real_route: records[i].get('real_route'),
                                            title: title,
                                            height: 300
                                        });
                                        routecomparewindow.add(routeCompareChartPanel);
                                    }
                                    routecomparewindow.doLayout();
                                    setTimeout(function() { routecomparewindow.setWidth(routecomparewindow.getWidth() + 20) }, 200);
                                }
                                else {
                                    msgWarning(_Message_NoDataInSelection);
                                }
                            }
                        }
                    });
                }
            }
        ]
    });


    var routecomparewindow = new Ext.Window({
        title: _TxtRouteDeviateCompareChart,
        id: 'route_compare_chart_window',
        iconCls: 'icon-report-polygon-fence',
        layout: {
            type: 'table',
            columns: 1
        },
        height: 550,
        width: 750,
        tbar: tbar,
        autoScroll: true,
        minimizable: true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(routecomparewindow);
            }
        },
        resizable:false
    });

    routecomparewindow.show();

}